package edu.chl.da.ejb.persistency;

import edu.chl.da.ejb.core.Airport;
import edu.chl.da.ejb.core.Destination;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;

/**
 *
 * @author Jakob
 */
@Stateless
public class AirportFacade implements IAirportFacade {
    @PersistenceContext(unitName = "airline_pu")
    private EntityManager em;

    @Override
    public void create(Airport airport) {
        em.persist(airport);
    }

    @Override
    public void edit(Airport airport) {
        em.merge(airport);
    }

    @Override
    public void remove(Airport airport) {
        em.remove(em.merge(airport));
    }

    @Override
    public Airport find(Object id) {
        return em.find(Airport.class, id);
    }

    @Override
    public List<Airport> findByDestination(Destination destination) {
        Query q = em.createQuery("SELECT c FROM Airport c WHERE c.destination1 = :destination");
        q.setParameter("destination", destination);
        return q.getResultList();
    }

    @Override
    public List<Airport> findAll() {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        cq.select(cq.from(Airport.class));
        return em.createQuery(cq).getResultList();
    }
}
